package com.sgb.kjwl.view.ui.adapter.self.packers;

import android.content.Context;
import android.graphics.Rect;

import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ItemSelfTeamMemberBinding;
import com.sgb.kjwl.model.entity.self.SelfTeamMemberEntity;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.ScreenUtil;
import com.swgk.core.util.StringUtil;

import java.util.List;

public class SelfTeamMembersAdapter extends BaseRecycleAdapter<SelfTeamMemberEntity> {

    private IRecyclerItemClick itemClick;

    public SelfTeamMembersAdapter(Context context, List<SelfTeamMemberEntity> datas) {
        super(context, R.layout.item_self_team_member, datas);
    }

    @Override
    public void convert(ItemViewHolder holder, final SelfTeamMemberEntity entity) {
        holder.setBinding(BR.viewModel, entity);
        ((ItemSelfTeamMemberBinding) holder.getmBinding()).btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (itemClick != null) {
                    itemClick.delete(entity);
                }
            }
        });
        // ((ItemSelfTeamMemberBinding) holder.getmBinding()).btnEdit.setVisibility(View.GONE);
        ((ItemSelfTeamMemberBinding) holder.getmBinding()).btnEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (itemClick != null) {
                    itemClick.edit(entity);
                }
            }
        });
        holder.getmBinding().getRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (itemClick != null) {
                    itemClick.onItemClick(entity);
                }
            }
        });
        //头像
        ImageView workerAvatar = holder.itemView.findViewById(R.id.image_logo);
        RequestOptions options = new RequestOptions();
        options.transforms(new CenterCrop(), new RoundedCorners(8));
        if (TextUtils.isEmpty(entity.getAvatarAddress())) {
            Glide.with(context).load(R.mipmap.worker_avatar).into(workerAvatar);
        } else {
            Glide.with(context).load(entity.getAvatarAddress()).apply(options).into(workerAvatar);
        }
        //年龄和工龄
        TextView TvWorkerInfo = holder.itemView.findViewById(R.id.worker_member_Info);
        TvWorkerInfo.setText(entity.getWorkingYear() + "年工龄");

        TextView tvPhone = holder.itemView.findViewById(R.id.tv_member_phones);
        tvPhone.setText(entity.getContactPhone());
        TextView certificateName = holder.itemView.findViewById(R.id.certificate_list);
        if (entity.getCertificateName() != null && entity.getCertificateName().size() > 0) {
            StringBuilder stringBuilder = new StringBuilder();
            for (String name : entity.getCertificateName()) {
                stringBuilder.append(name).append(",");
            }
            certificateName.setText(stringBuilder.substring(0, stringBuilder.length() - 1));
        } else {
            certificateName.setText("");
        }


    }

    private void updateRecycler(RecyclerView recycler, SelfTeamMemberEntity entity) {
        if (entity.getCertificates() == null || entity.getCertificates().size() == 0) {
            recycler.setVisibility(View.GONE);
        } else {
            recycler.setVisibility(View.VISIBLE);
            recycler.setLayoutManager(new GridLayoutManager(recycler.getContext(), 3));
            recycler.addItemDecoration(new RecyclerView.ItemDecoration() {
                @Override
                public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                    //不是第一个的格子都设一个左边和底部的间距
                    outRect.left = ScreenUtil.dip2px(5);
                    outRect.bottom = ScreenUtil.dip2px(5);
                    //由于每行都只有3个，所以第一个都是3的倍数，把左边距设为0
                    if (parent.getChildLayoutPosition(view) % 3 == 0) {
                        outRect.left = 0;
                    }
                }
            });
            recycler.setAdapter(new SelfTeamMemberPicAdapter(recycler.getContext(), entity.getCertificatePath()));
        }
    }

    public void setItemClick(IRecyclerItemClick itemClick) {
        this.itemClick = itemClick;
    }

    public interface IRecyclerItemClick {
        void delete(SelfTeamMemberEntity entity);

        void edit(SelfTeamMemberEntity entity);

        void onItemClick(SelfTeamMemberEntity entity);
    }
}
